﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class VVRKOPF
    {
        public void CHECKBYVKK(Dictionary<string, object> pm)
        {
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            SResult = VKK.VKKLOAD(PM);
            TBVKK = SResult.Tables[0].Copy();

            PM["RENR"] = pm["RENR"].ToString();
            SResult = LOAD(PM);
            TBVRK = SResult.Tables[0].Copy();
            foreach (DataRow row in TBVKK.Rows)
            {
                foreach (DataRow dwVRK in TBVRK.Rows)
                {
                    dwVRK["VRKZAHLBED"] = row["VKKZAHLBED"];
                    dwVRK["VRKKUNDNR"] = row["VKKKUNDNR"];
                    UpdateVRKOPF(dwVRK, app.company, 2);
                }
            }

            PM["RENR"] = pm["RENR"].ToString();
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            SResult = VKP.LOADBYVK(PM);
            TBVKP = SResult.Tables[0].Copy();

            PM["RENR"] = pm["RENR"].ToString();
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            VRP.CLEANBYVKK(PM);

            PM["RENR"] = pm["RENR"].ToString();
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            SResult = VRP.LOADBYVKK(PM);
            TBVRP = SResult.Tables[0].Copy();


            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            PM["VKZSA"] = "VK";
            SResult = VZB.LOADBYVKK(PM);
            TBVKZ = SResult.Tables[0].Copy();

            string ZOLLTNR = ("0:100").PadRight(20);
            foreach (DataRow dwvkp in TBVKP.Rows)
            {

                filter = "VRPRENR='" + PM["RENR"].ToString() + "' AND VRPAUFKRS='" + dwvkp["VKPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwvkp["VKPAUFLFD"].ToString() + "' AND VRPAUFPOS='" + dwvkp["VKPAUFPOS"].ToString() + "' AND VRPZOLLTNR='" + "0:100".PadRight(20) + "' ";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter, sort);
                if (rows.Length > 0) continue;
                dwZB = null;
                filter = "VKZAUFKRS='" + dwvkp["VKPAUFKRS"].ToString() + "' AND VKZAUFLFD='" + dwvkp["VKPAUFLFD"].ToString() + "' AND VKZAUFPOS='" + dwvkp["VKPAUFPOS"].ToString() + "'";
                rows = TBVKZ.Select(filter);
                foreach (DataRow row in rows)
                {
                    dwZB = row;
                }
                if (dwZB == null) continue;
                VRP.NEWBYVKP(dwvkp, dwZB, PM["RENR"].ToString());
            }
        }

       
        public void VVT2VKVR(Dictionary<string, object> pm, string RENR, int paybal)
        {
            try
            {
                if (paybal < 2) return;
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["POS"] = pm["POS"];
                PM["WERT"] = pm["WERT"];
                PM["DELDATE"] = pm["DELDATE"];
                PM["ROW"] = pm["ROW"];
                PM["QTY"] = pm["QTY"];
                PM["LDDATE"] = pm["LDDATE"];
                double qty = VVVKQTY(PM, RENR);
                if (qty == 0)
                    return;
                SplitByPT(PM, RENR, paybal);//产生提货款发票
                if (TBVVT == null) return;
                ratio = 0.01 * 0.01 * Convert.ToDouble(dwZA["ZABZBIN" + paybal].ToString());
                if (ratio == 0)
                    return;

                double VVTWERT = 0;
                DataRow dwVRP = null;
                DataRow dwVRMom = null;
                bool IsVVPayExists = false; 

                VVTWERT = VKP.CalculateGoodsValue((DataRow)PM["ROW"], qty);
                if (paybal == 2)
                {
                    PM["QTY"] = qty; 
                    dwVRMom = ResetBaseVR(PM);//根据实际数量，重新计算价值
                }

                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString()
                + "' AND VRPRENR='" + RENR + "' AND VRPZOLLTNR LIKE '2:%'";
                sort = " VRPREPOS ASC ";

                rows = TBVRP.Select(filter, sort);
                if (rows.Length > 0)
                {
                    dwVRP = rows[0];//需要中款
                    IsVVPayExists = true;
                }
                if (dwVRP == null)
                    return;
                if (dwVRP["VRPERLKZ"].ToString() == "J")//已经支付的不再处理
                    return;
                if (VVTWERT == 0) return;
                if (IsVVPayExists)
                {
                    dwVRMom["VRPWAWERT"] = Helper.Right("0000000000" + (VVTWERT * 1000).ToString(), 10);//货物的总价值更新为
                    dwVRP["VRPWAWERT"] = Helper.Right("0000000000" + (VVTWERT * 1000).ToString(), 10);//附加费考虑后的值 ;//净值
                    double value = Helpers.Helper.RoundX(VVTWERT * ratio, 1);
                    if (value < 0)
                        value = 0;
                    value += VRP.GETABADD(dwVRMom);
                    dwVRP["VRPGESWERT"] = Helper.Right("0000000000" + (value * 1000).ToString(), 10);//附加费考虑后的值
                }
                dwVRP["VRPMENGE"] = qty * 100;//更新发货数量
                VRP.UpdateVRPOS(dwVRP, 0);
                RESTMOM(PM, dwVRMom);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public double VVVKQTY(Dictionary<string, object> pm, string RENR)
        {
            double qty = 0;

            filter = "VVTAUFKRS='" + pm["KRS"].ToString() + "' AND VVTAUFLFD='" + pm["LFD"].ToString() + "' AND VVTAUFPOS='" + pm["POS"].ToString() + "'";
            rows = TBVVT.Select(filter);
            if (rows.Length == 0) return 0;
            DataRow dwVVT = TBVVT.Clone().Rows.Add();
            dwVVT.ItemArray = rows[0].ItemArray;
            dwVVT["VVTMTR"] = 0;
            dwVVT["VVTKG"] = 0;
            foreach (DataRow row in rows)
            {
                dwVVT["VVTMTR"] = Convert.ToDouble(dwVVT["VVTMTR"].ToString()) + Convert.ToDouble(row["VVTMTR"].ToString());
                dwVVT["VVTKG"] = Convert.ToDouble(dwVVT["VVTKG"].ToString()) + Convert.ToDouble(row["VVTKG"].ToString());
            }

            if (dwVVT["VVTLIEFSCH"].ToString() == "00000000") return qty;//未确认到发货单

            if (dwVVT["VVTME"].ToString().Trim() == "KG")
            {
                qty = 0.01 * Convert.ToDouble(dwVVT["VVTKG"].ToString());
            }
            else
            {
                qty = 0.01 * Convert.ToDouble(dwVVT["VVTMTR"].ToString());
            }
            if (qty == 0)
            {
                qty = (double)pm["QTY"];
            }
            return qty;
        }

        public void VVT2VKREST(Dictionary<string, object> pm, string RENR, int paybal)
        {
            try
            {
                if (paybal < 2) return;
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["POS"] = pm["POS"];
                PM["WERT"] = pm["WERT"];
                PM["DELDATE"] = pm["DELDATE"];
                PM["ROW"] = pm["ROW"];
                PM["QTY"] = pm["QTY"];
                PM["LDDATE"] = pm["LDDATE"];
                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString()
                + "' AND VRPRENR='" + RENR + "' AND VRPERLKZ=' ' AND (VRPZOLLTNR LIKE '2:%' OR VRPZOLLTNR LIKE '1:%')";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter);
                if (rows.Length > 0)
                {
                    return;
                }
                SplitByPT(PM, RENR, paybal);//产生尾款发票 
                #region=====3=
                
                DataRow dwVRP = null;
                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString()
                + "' AND VRPRENR='" + RENR + "' AND VRPZOLLTNR LIKE '3:%'";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter);
                if (rows.Length == 0)
                {
                    return;
                }
                dwVRP = rows[0];//需要尾款
                if (dwVRP["VRPERLKZ"].ToString() == "J")//已经支付的不再处理
                    return;
                #endregion
                DataRow dwVRMom = null;
              
                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString() + "' AND VRPZOLLTNR='" + "0:100".PadRight(20) + "' ";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter, sort);
                if (rows.Length == 0)
                {
                    return;
                }
                //dwVRMom = rows[0];

                double qty = VVVKQTY(PM, RENR);
                if (paybal == 3)
                {
                    PM["QTY"] = qty;
                    dwVRMom = ResetBaseVR(PM);//根据实际数量，重新计算价值
                }

                double VVTWERT = VKP.CalculateGoodsValue((DataRow)PM["ROW"], qty);//再次根据发货量，更新价值
                if (VVTWERT == 0) return;

                dwVRMom["VRPWAWERT"] = Helper.Right("0000000000" + (VVTWERT * 1000).ToString(), 10);//货物的总价值更新为
                PM["QTY"] = qty;
                RESTMOM(PM, dwVRMom);
                dwVRP["VRPMENGE"] = qty*100;//货物的总价值更新为 
                dwVRP["VRPGESWERT"] = dwVRMom["VRPGESWERT"];//货物的总价值更新为 
                dwVRMom["VRPGESWERT"] = 0;//附加费考虑后的值 ;//净值
                VRP.UpdateVRPOS(dwVRP, 0);
                VRP.UpdateVRPOS(dwVRMom, 0);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void VV2VKREST(Dictionary<string, object> pm)
        {

            PM["KRS"] = pm["KRS"];
            PM["LFD"] = pm["LFD"];
            PM["RENR"] = pm["RENR"];
            PM["ZAHLBED"] = pm["ZAHLBED"];
            PM["PAYPAL"] = pm["PAYPAL"];
            string RENR = pm["RENR"].ToString();
            string order = pm["ORDERNUM"].ToString();
            string _ZAHLBED = pm["ZAHLBED"].ToString();
            int paypal = (int)pm["PAYPAL"];
            PrepareVK(PM, true);
            if (TBZA == null)
                return;
            if (TBZA.Rows.Count == 0)
                return;
            dwZA = TBZA.Rows[0];
            int ZABANZAHLG = Convert.ToInt16(dwZA["ZABANZAHLG"].ToString());
            foreach (DataRow dwVKP in TBVKP.Rows)
            {
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = dwVKP["VKPAUFKRS"].ToString();
                PM["LFD"] = dwVKP["VKPAUFLFD"].ToString();
                PM["POS"] = dwVKP["VKPAUFPOS"].ToString();
                PM["WERT"] = dwVKP["VKPPOSWERT"].ToString();
                PM["DELDATE"] = dwVKP["VKPLIEFTERM"].ToString();
                PM["ROW"] = dwVKP;
                PM["QTY"] = 0.01 * Convert.ToDouble(dwVKP["VKPMENGE1"].ToString());
                PM["LDDATE"] = ZABANZAHLG;
                VVT2VKREST(PM, RENR, paypal);
            }
            PM["KUNDNR"] = pm["KUNDNR"];
            KD.KDUPDATE(PM);
        }
        public void VVVKVR(Dictionary<string, object> pm)
        {
            PM["KRS"] = pm["KRS"];
            PM["LFD"] = pm["LFD"];
            PM["RENR"] = pm["RENR"];
            PM["ZAHLBED"] = pm["ZAHLBED"];
            PM["PAYPAL"] = pm["PAYPAL"];
            string RENR = pm["RENR"].ToString();
            string order = pm["ORDERNUM"].ToString();
            string _ZAHLBED = pm["ZAHLBED"].ToString();
            int paypal = (int)pm["PAYPAL"];
            PrepareVK(PM, true);
            if (TBZA == null)
                return;
            if (TBZA.Rows.Count == 0)
                return;
            dwZA = TBZA.Rows[0];
            int ZABANZAHLG = Convert.ToInt16(dwZA["ZABANZAHLG"].ToString());
            foreach (DataRow dwVKP in TBVKP.Rows)
            {
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = dwVKP["VKPAUFKRS"].ToString();
                PM["LFD"] = dwVKP["VKPAUFLFD"].ToString();
                PM["POS"] = dwVKP["VKPAUFPOS"].ToString();
                PM["WERT"] = dwVKP["VKPPOSWERT"].ToString();
                PM["DELDATE"] = dwVKP["VKPLIEFTERM"].ToString();
                PM["ROW"] = dwVKP;
                PM["QTY"] = 0.01 * Convert.ToDouble(dwVKP["VKPMENGE1"].ToString());
                PM["LDDATE"] = ZABANZAHLG;
                VVT2VKVR(PM, RENR, paypal);
            }
            PM["KUNDNR"] = pm["KUNDNR"];
            KD.KDUPDATE(PM);
        }

        public void PrepareVK(Dictionary<string, object> pm, bool IsReset = false)
        {
            try
            {

                if (IsReset)
                    IsPrepared = false;
                if (IsPrepared)
                    return;

                PM["ZABNR"] = pm["ZAHLBED"]; ;
                SResult = ZA.GETZABED(PM);
                TBZA = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                SResult = VKP.LOADBYVK(PM);
                TBVKP = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["AUFTYP"] = "K";
                SResult = VRP.LOADBYVKK(PM);
                TBVRP = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["AUFTYP"] = "K";
                SResult = VVT.VVLOAD(PM);
                TBVVT = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["AUFTYP"] = "K";
                SResult = VV.VVLOAD(PM);
                TBVV = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["VKZSA"] = "VK";
                SResult = VZB.LOADBYVKK(PM);
                TBVKZ = SResult.Tables[0].Copy();
                IsPrepared = true;
            }
            catch (Exception ex)
            {
                IsPrepared = false;
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public void VKKPT(Dictionary<string, object> pm)
        {
            PM["KRS"] = pm["KRS"];
            PM["LFD"] = pm["LFD"];
            PM["RENR"] = pm["RENR"];
            PM["ZAHLBED"] = pm["ZAHLBED"];
            PM["PAYPAL"] = pm["PAYPAL"];
            string RENR = pm["RENR"].ToString();
            string order = pm["ORDERNUM"].ToString();
            string _ZAHLBED = pm["ZAHLBED"].ToString();
            int paypal = (int)pm["PAYPAL"];
            PrepareVK(PM, true);
            if (TBZA == null)
                return;
            if (TBZA.Rows.Count == 0)
                return;
            dwZA = TBZA.Rows[0];
            int ZABANZAHLG = Convert.ToInt16(dwZA["ZABANZAHLG"].ToString());

            foreach (DataRow dwVKP in TBVKP.Rows)
            {
                PM["KRS"] = dwVKP["VKPAUFKRS"].ToString();
                PM["LFD"] = dwVKP["VKPAUFLFD"].ToString();
                PM["POS"] = dwVKP["VKPAUFPOS"].ToString();
                PM["WERT"] = dwVKP["VKPPOSWERT"].ToString();
                PM["DELDATE"] = dwVKP["VKPLIEFTERM"].ToString();
                PM["ROW"] = dwVKP;
                PM["QTY"] = 0.01 * Convert.ToDouble(dwVKP["VKPMENGE1"].ToString());
                PM["LDDATE"] = ZABANZAHLG;
                SplitByPT(PM, RENR, paypal);
            }
            IsPrepared = false;
            PM["KUNDNR"] = pm["KUNDNR"];
            KD.KDUPDATE(PM);
        }


      

    } 
}
